package com.hw01.utils;

import java.time.LocalDate;

/**
 * 日期工具类
 */
public class DateUtils {

    // 获取当前年份
    public static String getYear() {
        return getYear(null);
    }

    // 获取年份
    public static String getYear(LocalDate date) {
        if (date == null) {
            date = LocalDate.now();
        }

        return String.valueOf(date.getYear());
    }

    // 获取当前月份
    public static String getMonth() {
        return getMonth(null);
    }

    // 获取月份
    public static String getMonth(LocalDate date) {
        if (date == null) {
            date = LocalDate.now();
        }
        String month = String.valueOf(date.getMonthValue());

        if (month.length() == 1) {
            month = "0" + month;
        }
        return month;
    }

    // 补 0
    public static String mendMonth(String month) {
        if (month == null || "".equals(month)) {
            return getMonth();
        }
        if (month.length() == 1) {
            return "0" + month;
        }
        return month;
    }

}
